package com.dayatang.exam.domain;

import java.util.List;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.dayatang.domain.AbstractEntity;
import com.dayatang.domain.QuerySettings;

/**
 * 试题。有单�?题�?多�?题�?判断题�?填空题�?问答题等子类
 * 
 * @author yang
 * 
 */
@Entity
@Table(name = "QUESTIONS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "CATEGORY", discriminatorType = DiscriminatorType.STRING)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public abstract class Question extends AbstractEntity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -810984087274283028L;

	public static final String TYPE_SINGLE_CHOICE = "SC";	//单�?�?
	
	public static final String TYPE_MULTI_CHOICE = "MC";	//多�?�?
	
	public static final String TYPE_JUDGEMENT = "J";		//判断�?
	
	public static final String TYPE_FILL_BLANK = "FB";		//填空�?
	
	public static final String TYPE_QUIZ = "Q";				//问答�?
	
	
	/**
	 * 题目内容
	 */
	@Lob
	private String topic;
	
	/**
	 * 难度
	 */
	private int difficuty;
	
	/**
	 * 业务类别
	 */
	private String questionCategory;
	
	/**
	 * 标准答案或参考答�?
	 */
	@Lob
	private String solution;
	
	/**
	 * �?��题库
	 */
	@ManyToOne
	@JoinColumn(name = "LIB_ID", nullable = false)
	private QuestionLib questionLib;

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public int getDifficuty() {
		return difficuty;
	}

	public void setDifficuty(int difficuty) {
		this.difficuty = difficuty;
	}

	public String getQuestionCategory() {
		return questionCategory;
	}

	public void setQuestionCategory(String questionCategory) {
		this.questionCategory = questionCategory;
	}

	public String getSolution() {
		return solution;
	}

	public void setSolution(String solution) {
		this.solution = solution;
	}

	public QuestionLib getQuestionLib() {
		return questionLib;
	}

	public void setQuestionLib(QuestionLib questionLib) {
		this.questionLib = questionLib;
	}

	/**
	 * 返回题目类型的名�?
	 * @return
	 */
	public abstract String getTypeName();
	
	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof Question))
			return false;
		Question castOther = (Question) other;
		return new EqualsBuilder().append(topic, castOther.topic).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(topic).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", getId()).append("topic", topic).toString();
	}
	

	public static Question get(Long id) {
		return getRepository().get(Question.class, id);
	}
	
	public static List<Question> findByTopic(String topic) {
		return getRepository().find(QuerySettings.create(Question.class).containsText("topic", topic));
	}
	public static <T extends Question> List<T> findInLib(QuestionLib questionLib, Class<T> questionClass) {
		return getRepository().find(QuerySettings.create(questionClass).eq("questionLib", questionLib));
	}
}
